package com.mybatis.utlis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * 生成工厂类
 *
 * @author 24040
 * @name SqlSessionFactoryUtil
 * @date 2022-08-05 12:02
 */
public class SqlSessionFactoryUtil {

    private static SqlSessionFactory  factory;
    static {
        //读取MyBatis的核心配置文件
        try {
            Reader resource= Resources.getResourceAsReader("mybatis-config.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            factory = sqlSessionFactoryBuilder.build(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory(){
        return factory;
    }


    public static SqlSession getSqlSession(){
        return factory.openSession(false);
    }
}
